<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<?php


/*
 *  A Part of snock project
 *   Author king-of-war
 * Last modified 2009-5-12
 *  designed for sort operation
 */
class temp {
	private $root_path;

	function __construct($url) {
		/*
		* load connection class
		*/
		include_once $url . 'includes/conn.php';
		/*
		 * load xml class
		 */
		include_once $url . 'includes/xml/xml_op.php';
		$this->set_root_path($url);

	}
	/*
	 * select the temp date from database
	 */
	function temp_get() {
		$conn = new conn();
		$sql = 'select * from temp_set';
		$conn->set_sql($sql);
		$line = $conn->get_line();
		$temps = array ();
		if ($line) {
			$result = $conn->get_result();
			$row = $result->fetch_assoc();
			$temps['news_image'] = $row['news_image'];
			$temps['news_data'] = $row['news_data'];
			$temps['pros_image'] = $row['pros_image'];
			$temps['pros_data'] = $row['pros_data'];
			$temps['guest_num'] = $row['guest_num'];
			$temps['replay_num'] = $row['replay_num'];
			$temps['flink_num'] = $row['flink_num'];
			$temps['vote_num'] = $row['vote_num'];


		} else {
			echo 'Error: Config data lost';
			//	exit;
		}

		return $temps;
	}
	/*
	 * update site config data
	 */
	function temp_updata($news_image,$news_data,$pros_image,$pros_data,$guest_num,$replay_num,$flink_num,$vote_num) {
		$conn = new conn();
		$line = 0;
		$sql = sprintf("update temp_set set news_image='%d',news_data='%d',pros_image='%d',pros_data='%d',guest_num='%d',replay_num='%d',flink_num='%d',vote_num='%d'",$news_image,$news_data,$pros_image,$pros_data,$guest_num,$replay_num,$flink_num,$vote_num);
		$conn->set_sql($sql);
		$line = $conn->get_line();
		if ($line) {
			$this->xml_write($news_image,$news_data,$pros_image,$pros_data,$guest_num,$replay_num,$flink_num,$vote_num);
		}else{
			$line=0;
		}
		return $line;
	}
	/*
	 * write to xml
	 */
	function xml_write($news_image,$news_data,$pros_image,$pros_data,$guest_num,$replay_num,$flink_num,$vote_num) {

		$url = $this->get_root_path();
		$xml_op = new xml_op($url);
		$file = 'includes/xml/temp_set.xml';
		if (file_exists($url . $file)) {
			$xml = simplexml_load_file($url . $file);
			/*
			 * write to xml
			 */
			$xml->temp[0]->news_image = $news_image;
			$xml->temp[0]->news_data = $news_data;
			$xml->temp[0]->pros_image = $pros_image;
			$xml->temp[0]->pros_data = $pros_data;
			$xml->temp[0]->guest_num = $guest_num;
			$xml->temp[0]->replay_num = $replay_num;
			$xml->temp[0]->flink_num = $flink_num;
			$xml->temp[0]->vote_num = $vote_num;
			$contant = $xml->asXML();
			$xml_op->xml_creat($file, $contant);
			return true;
		} else {
			echo 'File open error <br/>Check the file please';
			return false;
			exit;
		}

	}

	/*
	 * read xml
	 */
	function xml_read($key) {

		$url = $this->get_root_path();
		$xml_op = new xml_op($url);
		$file = 'includes/xml/temp_set.xml';
		if (file_exists($url . $file)) {

			$xml = simplexml_load_file($url . $file);

			/*
			 * read xml
			 */

			$result = $xml->temp[0]-> $key;

			return $result;
		} else {
			echo 'File open error <br/>Check the file please';
			return null;
			exit;
		}

	}

	/*
	 * function for vars which used above
	 */
	function set_root_path($url) {
		$this->root_path = $url;
	}
	function get_root_path() {
		return $this->root_path;
	}

}
?>
